<template>
  <div class="reset-password-outer">
    <div class="reset-password-card">
      <div class="reset-email-tip">
        正在重置账号：<span>{{ route.query.email }}</span>
      </div>
      <el-form :model="form" :rules="rules" ref="formRef" class="reset-form">
        <el-form-item label="新密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入新密码" show-password />
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入新密码" show-password />
        </el-form-item>
        <el-form-item>
          <el-button
            type="success"
            :loading="loading"
            @click="onSubmit"
            class="reset-btn"
            >重置密码</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup name="ResetPassword">
import { ref, reactive } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { resetPassword } from '@/api/login';
import { useRouteStore } from '@/store/route';
const route = useRoute();
const router = useRouter();
const formRef = ref();
const loading = ref(false);
const routeStore = useRouteStore();
const form = reactive({
  password: '',
  confirmPassword: ''
});

const validateConfirmPassword = (rule: any, value: string, callback: any) => {
  if (value !== form.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

const rules = {
  password: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入新密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ]
};

const onSubmit = () => {
  if (!formRef.value) return;
  formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true;
      try {
        await resetPassword({
          email: route.query.email,
          resetToken: route.query.token,
          newPassword: form.password
        });
        ElMessage.success('密码重置成功，正在跳转至登录页面...');
        
        const query = routeStore.routeQuery;
        router.push({ path: '/login', query:query });
      } catch (e) {
        console.log(e,"=========");
      } finally {
        loading.value = false;
      }
    }
  });
};
</script>

<style scoped>
.reset-password-outer {
  /* min-height: 100vh; */
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.reset-password-card {
  max-width: 400px;
  width: 100%;
  padding: 32px 24px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 32px rgba(27, 31, 35, 0.07);
}
.reset-form .el-form-item {
  display: flex;
  align-items: center;
}
.reset-btn {
  width: 100%;
}
@media (max-width: 600px) {
  .reset-password-card {
    padding: 24px 8px;
  }
}
.reset-email-tip {
  text-align: center;
  margin-bottom: 18px;
  color: #888;
  font-size: 15px;
  word-break: break-all;
}
.reset-email-tip span {
  color: #2da44e;
  font-weight: bold;
}
</style>
